package com.pi.stroop.controller.facade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pi.stroop.controller.vo.MemberPayRecordVO;
import com.pi.stroop.dao.entity.MemberPayRecordEntity;
import com.pi.uc.dao.entity.UcUserEntity;
import com.pi.uc.service.UcUserService;

@Service
public class StroopPayFacade {
  @Autowired
  private UcUserService userService;
  
  public List<MemberPayRecordVO> translateMemberPayRecordRecord(List<MemberPayRecordEntity> list){
    List<MemberPayRecordVO> voList = new ArrayList<>();
    if(null == list || list.isEmpty()) {
      return voList;
    }
    List<Long> userIds = new ArrayList<>();
    for(MemberPayRecordEntity entity:list) {
      userIds.add(entity.getMemberId());
    }
    Map<Long, UcUserEntity> map = new HashMap<>();
    if(!userIds.isEmpty()) {
      List<UcUserEntity> userList = userService.queryUserByIds(userIds);
      map = userList.stream().collect(
          Collectors.toMap(UcUserEntity::getId, Function.identity(), (key1, key2) -> key2, HashMap:: new));
    }
    for(MemberPayRecordEntity entity:list) {
      MemberPayRecordVO vo = new MemberPayRecordVO();
      try {
        BeanUtils.copyProperties(vo, entity);
      } catch (Exception e) {}
      vo.setUserName(map.containsKey(entity.getMemberId())?map.get(entity.getMemberId()).getName():"");
      voList.add(vo);
    }
    return voList;
  }
}
